/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.controller;

import com.apache.action.PlatFromAction;
import com.apache.api.api.ApiService;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.client.LogClient;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.info.manager.PubMetadataManager;
import com.apache.tools.ConfigUtil;
import com.apache.tools.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  信息操作action
 *
 * @author iussoft 创建时间：2016-10-14
 */
@Controller
@RequestMapping("/info/operate/")
public class PubOperateInfoAction extends PlatFromAction {

    @Autowired
    private ApiService infoService;

    @Autowired
    private PubMetadataManager pubMetadataManager;

    /**
     * 功能说明：通用跳转到列表查询页面
     */
    @RequestMapping("operate.action")
    public ModelAndView operate(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(LIST);
        view.addObject("sortId", request.getParameter("sortId"));

        return view;
    }

    /**
     * 功能说明：通用跳转到列表查询页面
     */
    @RequestMapping("list.action")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("info/infos");
        return view;
    }

    @RequestMapping("list!data.action")
    @ResponseBody
    protected Object data(HttpServletRequest request, String page, String rows) {
        String sortId = request.getParameter("sortId");
        ParamsVo vo = new ParamsVo();
        vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
        vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
        vo.setParams("sortCode", sortId);
        if (Validator.isNotNull(request.getParameter("status"))) {
            vo.setParams("status", request.getParameter("status"));
        }
        vo.setMethodKey("selectInfo");
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = infoService.doService(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            Page pageList = (Page) entity.getEntity();
            mapData.put("total", pageList.getCount());
            mapData.put("rows", pageList.getPageObjects());
        }
        request.setAttribute("sortId", request.getParameter("sortId"));
        return mapData;
    }

    @RequestMapping("init!check.action")
    public ModelAndView checkPass(HttpServletRequest request, String infoId, String sortId) {
        ModelAndView view = new ModelAndView("info/info-check-save");
        ParamsVo vo = new ParamsVo();
        vo.setParams("sortCode", sortId);
        vo.setParams("infoId", infoId);
        vo.setMethodKey("selectSingle");
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = infoService.doService(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            mapData = (Map<String, Object>) entity.getEntity();
        }
        mapData.put("sortId", sortId);
        view.addObject("item", mapData);
        return view;
    }

    @RequestMapping("doCheck.action")
    @ResponseBody
    public ResultMsg doCheck(HttpServletRequest request) {
        ResultMsg rmsg = new ResultMsg("T", "审核失败！");
        ParamsVo vo = new ParamsVo();
        vo.setParams("sortCode", request.getParameter("sortId"));
        vo.setParams("infoId", request.getParameter("infoId"));
        vo.setParams("infoLevel", request.getParameter("infoLevel"));
        vo.setMethodKey("updateInfo");
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        vo.setParams("modifyUser",
                request.getParameter("modifyUser"));//this.getLogingUser(request).getUserEname());
        vo.setParams("modifyTime", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        vo.setParams("status", "1");
        ResultEntity entity = infoService.doService(vo);
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            rmsg = new ResultMsg("T", "审核成功");
            Map<String, Object> logMap = new HashMap<String, Object>();
            logMap.put("logOperationType", "check");
            logMap.put("logClassName", "com.apache.pubInfo.check_info");
            logMap.put("logIp", this.getUserIp(request));
            logMap.put("log_userEname", request.getParameter("modifyUser"));
            logMap.put("infoid", request.getParameter("infoId"));
            logMap.put("title", request.getParameter("title"));
            logMap.put("logcontent", "审核通过，审核意见：" + request.getParameter("checkContent"));
            LogClient.getInstance().execute(logMap);//添加日志
        }
        return rmsg;
    }

    @RequestMapping("doPsCheck.action")
    @ResponseBody
    public ResultMsg doPsCheck(HttpServletRequest request) {
        ResultMsg rmsg = new ResultMsg("T", "操作失败！");
        ParamsVo vo = new ParamsVo();
        vo.setParams("sortCode", request.getParameter("sortId"));
        vo.setParams("infoId", request.getParameter("infoId"));
        vo.setParams("infoLevel", request.getParameter("infoLevel"));
        vo.setMethodKey("updateInfo");
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        vo.setParams("modifyUser",
                request.getParameter("modifyUser"));//this.getLogingUser(request).getUserEname());
        vo.setParams("modifyTime", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        vo.setParams("status", "-1");
        ResultEntity entity = infoService.doService(vo);
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            rmsg = new ResultMsg("T", "操作成功");
            Map<String, Object> logMap = new HashMap<String, Object>();
            logMap.put("logOperationType", "check");
            logMap.put("logClassName", "com.apache.pubInfo.check_info");
            logMap.put("logIp", this.getUserIp(request));
            logMap.put("log_userEname", request.getParameter("modifyUser"));
            logMap.put("infoId", request.getParameter("infoId"));
            logMap.put("title", request.getParameter("title"));
            logMap.put("logContent", "信息驳回，审核意见：" + request.getParameter("checkContent"));
            LogClient.getInstance().execute(logMap);//添加日志
        }
        return rmsg;
    }

    protected BaseManager getBaseManager() {
        return null;
    }

    protected String getPrefix() {
        return "info/check-info-";
    }

}
